<template>
  <view>
    <view class="search-box">
      <uni-search-bar @confirm="search" @input="input" cancelButton="none" focus="true" radius="100"></uni-search-bar>
    </view>
    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="searchResult.length !== 0">
      <view class="sugg-item" v-for="(item,i) in searchResult" :key="item.goods_id" @click="gotoGoodsDetail(item)">
        <text class="goods-name">{{item.goods_name}}</text>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clearHistory"></uni-icons>
      </view>
      <view class="history-list">
        <view class="history-item" v-for="(item,i) in historys" :key="i">
          <uni-tag :text="item" @click="gotoGoodsList(item)"></uni-tag>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null, //延时器
        kw: '', //搜索关键词
        searchResult: [],
        historyList: []
      };
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    computed: {
      historys() {
        return [...this.historyList].reverse()
      }
    },
    methods: {
      input(e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e
          this.getSearchList()
        }, 500)
      },
      async getSearchList() {
        if (this.kw === '') {
          this.searchResult = []
          return
        }
        const {
          data: res
        } = await uni.$http.get('/goods/qsearch?query=' + this.kw)
        if (res.meta.status !== 200) return uni.$ShowMsg()

        this.searchResult = res.message

        this.saveSearchResult(this.kw)
      },
      async gotoGoodsDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },
      // 保存搜索结果
      saveSearchResult(kw) {
        if (!this.historyList.includes(kw)) {
          this.historyList.push(kw)
        }
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
      clearHistory() {
        this.historyList = []
        uni.setStorageSync('kw', '[]')
      },
      gotoGoodsList(kw) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + kw
        })
      }
    }
  }
</script>

<style lang="scss">
  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-name {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        margin-right: 3px;
        color: limegreen;
      }
    }
  }

  .history-box {
    padding: 0px 10px;

    .history-title {
      display: flex;
      justify-content: space-between;
      height: 40px;
      align-items: center;
      border-bottom: 1rpx solid hotpink;
      font-size: 13px;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      .history-item {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>